package com.javaPro.myProject.modules.comment.controller;

import com.javaPro.myProject.common.controller.BaseController;
import com.javaPro.myProject.common.model.AjaxResult;
import com.javaPro.myProject.common.model.ListByPage;
import com.javaPro.myProject.modules.comment.entity.TComment;
import com.javaPro.myProject.modules.comment.service.TCommentService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 评论表(TComment)表控制层
 *
 * @author makejava
 * @since  16:35:52
 */
@RestController
@RequestMapping("tComment")
public class TCommentController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private TCommentService tCommentService;

    /**
     * 分页查询
     *
     * @param tComment 筛选条件
     * @return 查询结果
     */
    @GetMapping("list")
    public ListByPage queryByPage(TComment tComment) {
        startPage();
        List<TComment> tComments = this.tCommentService.queryByPage(tComment);
        return getList("",tComments);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<TComment> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.tCommentService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param tComment 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public AjaxResult add(TComment tComment) {
        int insert = this.tCommentService.insert(tComment);
        if (insert == -1){
            return AjaxResult.error("自己的讨论不能评论~");
        }
        return AjaxResult.autoJudge(insert);
    }

    /**
     * 编辑数据
     *
     * @param tComment 实体
     * @return 编辑结果
     */
    @PostMapping("edit")
    public ResponseEntity<TComment> edit(TComment tComment) {
        return ResponseEntity.ok(this.tCommentService.update(tComment));
    }



}

